package cn.yhp.mybatis.binding;

import cn.yhp.mybatis.session.SqlSession;

import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @author yhp
 * @description mapper映射器代理工厂，用于生成mapper代理对象
 * @date 2023/11/8
 */
public class MapperProxyFactory<T> {

    private final Class<T> mapperInterface;

    private Map<Method, MapperMethod> methodCache = new ConcurrentHashMap<Method, MapperMethod>();

    public MapperProxyFactory(Class<T> mapperInterface){
        this.mapperInterface=mapperInterface;
    }

    /**
     * 创建mapper接口mapperInterface的代理对象
     * @param sqlSession
     * @return
     */
    public T newInstance(SqlSession sqlSession){
        final MapperProxy<T> mapperProxy = new MapperProxy<>(sqlSession,mapperInterface,methodCache);
        return (T)Proxy.newProxyInstance(mapperInterface.getClassLoader(),new Class[]{mapperInterface},mapperProxy);
    }


}
